{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为便于根据岭参数的范围获得对应的岭迹曲线（每个回归系数对应一条曲线），现用Python编写函数plot_ridge_curve实现岭迹曲线的绘制功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEJCAYAAABohnsfAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nO3deXxU9b3/8dcnG0mAkBACQRYTKGgRMWC4BS2CwkWsWIEqaqkWqVvr0lZv69Lbe392k+vDKq21F7FWqxdrURFcEKygAopLIIGCCLJECAKSsJOQ9fv7YyZj9kxgMmdC3s/H4zxy5nzPzHxOMjnvOevXnHOIiIgEI8rrAkREpO1QaIiISNAUGiIiEjSFhoiIBE2hISIiQYvxuoDW1q1bN5eRkeF1GSIibcrq1asLnXNpdaef8qGRkZFBTk6O12WIiLQpZvZ5Q9O1e0pERIKm0BARkaApNEREJGin/DENEZFglZeXU1BQwPHjx70uJWzi4+Pp3bs3sbGxQc2v0BAR8SsoKKBz585kZGRgZl6X0+qccxQVFVFQUEBmZmZQz9HuKRERv+PHj5OamtouAgPAzEhNTW3RlpVCQ0SkhvYSGNVaurwKjUbs2LGDzz77zOsyREQiikKjEfPnz+fRRx/1ugwRkYii0GhEz5492bNnj9dliIhEFIVGI9LT0xUaIuKZ//qv/+Lss89m4MCBzJkzp9H5SkpKGD16NJWVlQAsXbqUa6+9NtBeVlbGBRdcQEVFRUjqUmg0QqEhIl5ZsmQJubm55OXl8dJLL7FgwYJG5/3rX//KlClTiI6OBmDt2rUMHTo00B4XF8fYsWP5xz/+EZLaFBqNUGiIiFdeeeUVpk+fTnl5OX/605/4zne+0+i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IB8zsfeDf8PWK+IbX9YgES8c0RMLMzC4CzsF319G9Hpcj0iLa0hAJIzM7B18f1j/F149HJ+fcxd5WJRI8hYZImPjPmFoFzHbO/crMBuPr+Ogi59w7nhYnEiSFhkgYmFlX4D1guXPu5hrT/wH0dc6N9Kw4kRZQaIiISNB0IFxERIKm0BARkaApNEREJGgKDRERCZpCQ0REgqbQEBGRoCk0REQkaAoNEREJ2v8HEmEerqzpjvgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "# 以下 font.family 设置仅适用于 Mac系统，其它系统请使用对应字体名称\n",
    "matplotlib.rcParams['font.family'] = 'Arial Unicode MS'\n",
    "\n",
    "def plot_ridge_curve(X,y,plist,kmax=1,qnum=10,intercept=True):\n",
    "    \"\"\"\n",
    "    绘制岭迹曲线\n",
    "    X : 自变量的数据矩阵\n",
    "    y : 响应变量向量或矩阵\n",
    "    plist : 选择显示的系数列表\n",
    "    kmax : 岭参数的最大值\n",
    "    qnum : 将0~kmax的区间分成qnum等分\n",
    "    intercept : 是否计算戴距\n",
    "    \"\"\"\n",
    "    if intercept:\n",
    "        X = np.c_[X,[1]*X.shape[0]]\n",
    "    \n",
    "    coefs = []\n",
    "    for k in np.linspace(0,kmax,qnum+1):\n",
    "        coefs.append(np.matmul(np.matmul(np.linalg.inv(np.matmul(X.T,X)+k*np.identity(X.shape[1])),X.T),y))\n",
    "    \n",
    "    coefs = np.array(coefs)\n",
    "    plt.axhline(0,0,kmax,linestyle='--',c='gray')\n",
    "    plt.axhline(np.mean(coefs[:,plist]),0,kmax,linestyle='--',c='gray')\n",
    "    \n",
    "    for p in plist:\n",
    "        plt.plot(np.linspace(0,kmax,qnum+1),coefs[:,p],'-',label=r\"$\\beta_\"+str(p+1)+\"(k)$\",color='black',linewidth=p+1)\n",
    "    plt.xlabel(r\"$x$\",fontsize=14)\n",
    "    plt.ylabel(r\"$\\beta(k)$\",fontsize=14)\n",
    "    plt.legend()\n",
    "    plt.show()\n",
    "    \n",
    "import pandas as pd\n",
    "out = pd.read_csv(\"http://image.cador.cn/data/demo.614.csv\")\n",
    "X = out.drop(columns='y').values\n",
    "y = out.y.values\n",
    "plot_ridge_curve(X,y,[0,1],kmax=1,qnum=100)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
